package com.servlet;

import com.bean.User;
import com.service.UserService;
import com.service.impl.UserServiceImpl;


import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

import static com.google.code.kaptcha.Constants.KAPTCHA_SESSION_KEY;

@WebServlet("/user.do")
public class UserServlet extends BaseServlet {
    private UserService userService = new UserServiceImpl();

    /**
     * 处理登录的功能
     *
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void login(HttpServletRequest req, HttpServletResponse resp) throws ServletException,
            IOException {
        //  1、获取请求的参数
        String username = req.getParameter("userName");
        String password = req.getParameter("userpassword");
//        String userphone =req.getParameter("userphone");
        // 调用 userService.login()登录处理业务
        User loginUser = userService.login(new User(null, username, password, null,null,null,null));
        // 如果等于null,说明登录 失败!
        if (loginUser == null) {
            // 把错误信息，和回显的表单项信息，保存到Request 域中
            req.setAttribute("msg", "用户或密码错误！");
            req.setAttribute("username", username);
            //   跳回登录页面
            req.getRequestDispatcher("index1.jsp").forward(req, resp);
        } else {
            // 登录 成功
            //跳到成功页面 login_success.html
            req.getSession().setAttribute("user", loginUser);
            req.getRequestDispatcher("login_success.jsp").forward(req, resp);
        }
    }

    /**
     * 处理注册的功能
     *
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void regist(HttpServletRequest req, HttpServletResponse resp) throws ServletException,
            IOException {
        //  1、获取请求的参数
        String userName = req.getParameter("userName");
        String userpassword = req.getParameter("userpassword");
        String useremail = req.getParameter("useremail");
        String userphone = req.getParameter("userphone");
        String userpic = req.getParameter("userpic");
        String code = req.getParameter("code");//验证码
//        2、检查 验证码
        // 获取Session 中的验证码
        String token = (String) req.getSession().getAttribute(KAPTCHA_SESSION_KEY);
        // 删除 Session 中的验证码
        req.getSession().removeAttribute(KAPTCHA_SESSION_KEY);
        if (token.equalsIgnoreCase(code)) {
//        3、检查 用户名是否可用
            if (userService.existsUsernameemail(userName,useremail)) {
                System.out.println("用户名[" + userName + "]已存在!");
                System.out.println("邮箱" +useremail+"已存在");
                // 把回显信息，保存到Request 域中
                req.setAttribute("msg", "用户名或邮箱已存在！！");
                req.setAttribute("userName", userName);
                req.setAttribute("userpassword",userpassword);
                req.setAttribute("useremail", useremail);
                req.setAttribute("userphone",userphone);
                req.setAttribute("userpic",userpic);
//                创建一个Session对象，保存信息
                HttpSession session = req.getSession();
                session.setAttribute("userName",userName);
                session.setAttribute("userpassword",userpassword);
                session.setAttribute("useremail",useremail);
                session.setAttribute("userphone",userphone);
                session.setAttribute("userpic",userpic);
                session.setAttribute("code",code);
//        跳回注册页面
                req.getRequestDispatcher("page/zhuce/signin.jsp").forward(req, resp);
            } else {
                //      可用
//                调用Sservice 保存到数据库
                userService.registUser(new User(null, userName, userpassword, useremail,userphone,1,null));
//
//        跳到注册成功页面 regist_success.jsp
                req.getRequestDispatcher("index1.jsp").forward(req, resp);
            }
        } else {
            // 把回显信息，保存到Request 域中
            req.setAttribute("msg", "验证码错误！！");
            req.setAttribute("userName", userName);
            req.setAttribute("useremail", useremail);
            System.out.println("验证码[" + code + "]错误");
            HttpSession session = req.getSession();
            session.setAttribute("userName",userName);
            session.setAttribute("userpassword",userpassword);
            session.setAttribute("useremail",useremail);
            session.setAttribute("userphone",userphone);
            session.setAttribute("userpic",userpic);
            session.setAttribute("code",code);
            req.getRequestDispatcher("page/zhuce/signin.jsp").forward(req, resp);
        }

//        HttpSession session = req.getSession();
//        String userName = req.getParameter("userName");
//        String userpassword = req.getParameter("userpassword");
//        if ()

    }

    /**
     * 注销
     *
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void logout(HttpServletRequest req, HttpServletResponse resp) throws ServletException,
            IOException {
        //1.销毁Session中用户登陆的信息
        req.getSession().invalidate();
        //2.重定向到首页
        resp.sendRedirect(req.getContextPath());
    }
}
